package com.lzf.yh.userService.impl;

import com.lzf.yh.commons.DateUtil;
import com.lzf.yh.commons.JsonBean;
import com.lzf.yh.dao.GuitarInfoMapper;
import com.lzf.yh.dao.OrderInfoMapper;
import com.lzf.yh.dao.Recommend_guitarMapper;
import com.lzf.yh.dao.UsergetgoodsMapper;
import com.lzf.yh.entity.GuitarInfo;
import com.lzf.yh.entity.OrderInfo;
import com.lzf.yh.entity.Recommend_guitar;
import com.lzf.yh.entity.Usergetgoods;
import com.lzf.yh.userService.OrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

/**
 * @Auther: Wyz
 * @Date: 2021/4/17 13:24
 * @Description:
 */
@Service
public class OrderInfoServiceImpl implements OrderInfoService {

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Autowired
    private GuitarInfoMapper guitarInfoMapper;

    @Autowired
    private Recommend_guitarMapper recommend_guitarMapper;

    @Autowired
    private UsergetgoodsMapper usergetgoodsMapper;

    @Override
    public JsonBean insertOrderInfo(OrderInfo orderInfo) {
        String date = DateUtil.convert2String(new Date(), "yyyy-MM-dd HH:mm:ss");
        String split = date.split(":")[0]+":"+date.split(":")[1]+":00";
        String orderID = System.currentTimeMillis()+ UUID.randomUUID().toString().replaceAll("-","").substring(0,30);
        orderInfo.setOrderId(orderID);
        orderInfo.setDealtime(split);
        orderInfo.setPrace(orderInfo.getPrace()*orderInfo.getCount());
        JsonBean jsonBean = new JsonBean(-1, "创建订单失败，请重新下单", null);
        //判断他是否是推荐商品
        if (orderInfo.getGoodsType().equals("0")){//普通商品
            GuitarInfo guitarInfo = guitarInfoMapper.selectByPrimaryKey(orderInfo.getGuitarid());
            if (guitarInfo.getCount()<orderInfo.getCount()){
                jsonBean = new JsonBean(-1, "商品数量不足，请重新下单", null);
            }else {
                int insert = orderInfoMapper.insert(orderInfo);
                if (insert>0){
                    //修改商品数量
                    GuitarInfo guitarInfo1 = new GuitarInfo();
                    guitarInfo1.setCount(guitarInfo.getCount()-orderInfo.getCount());
                    guitarInfo1.setName(orderInfo.getGuitarname());
                    int i = guitarInfoMapper.updateCountByName(guitarInfo1);
                    Recommend_guitar recommend_guitar = new Recommend_guitar();
                    recommend_guitar.setCount(guitarInfo.getCount()-orderInfo.getCount());
                    recommend_guitar.setName(orderInfo.getGuitarname());
                    int i1 = recommend_guitarMapper.updateCountByName(recommend_guitar);
                    if (i>0){
                        jsonBean = new JsonBean(0, "", orderID);
                    }
                }

            }
        }else {//推荐商品
            Recommend_guitar recommend_guitar = recommend_guitarMapper.selectByPrimaryKey(orderInfo.getGuitarid());
            if (recommend_guitar.getCount()<orderInfo.getCount()){
                jsonBean = new JsonBean(-1, "商品数量不足，请重新下单", null);
            }else {
                int i2 = orderInfoMapper.insert(orderInfo);
                if (i2>0){
                    //修改商品数量
                    GuitarInfo guitarInfo1 = new GuitarInfo();
                    guitarInfo1.setCount(recommend_guitar.getCount()-orderInfo.getCount());
                    guitarInfo1.setName(recommend_guitar.getName());
                    int i = guitarInfoMapper.updateCountByName(guitarInfo1);
                    Recommend_guitar recommend_guitar1 = new Recommend_guitar();
                    recommend_guitar1.setCount(recommend_guitar.getCount()-orderInfo.getCount());
                    recommend_guitar1.setName(recommend_guitar.getName());
                    int i1 = recommend_guitarMapper.updateCountByName(recommend_guitar1);
                    if (i>0){
                        jsonBean = new JsonBean(0, "1", orderID);
                    }
                }
            }
        }
        return jsonBean;
    }

    @Override
    public JsonBean updateOrderInfoStartsToOneByOrderInfoId(String orderid,Integer userid) {
        JsonBean jsonBean = new JsonBean(-1, "够买失败，请重新支付", null);
        Usergetgoods usergetgoods = usergetgoodsMapper.selectByPrimaryKey(userid);
        OrderInfo orderInfo = orderInfoMapper.selectByOrderid(orderid);
        if (usergetgoods.getBalance()<orderInfo.getPrace()){
            jsonBean = new JsonBean(-1, "余额不足，请充值后重新支付", null);
        }else {
            //根据id修改余额
            Usergetgoods usergetgoods1 = new Usergetgoods();
            usergetgoods1.setUserid(userid);
            usergetgoods1.setBalance(usergetgoods.getBalance()-orderInfo.getPrace());
            int i = usergetgoodsMapper.updateBalanceByUserid(usergetgoods1);
            //修改状态为已支付
            int i1 = orderInfoMapper.updateStartsByOrderidToOne(orderid);
            //成功返回的信息
            if (i>0&&i1>0){
                jsonBean = new JsonBean(0, "支付成功", null);
            }
        }
        return jsonBean;
    }

    @Override
    public JsonBean selectAllOrderInfoByUserID(Integer userid) {
        return new JsonBean(0,"",orderInfoMapper.selectAllOrderInfoByUserID(userid));
    }

    @Override
    public JsonBean updateOrderInfoStartsToThreeByOrderInfoId(String orderId) {
        JsonBean jsonBean = new JsonBean(-1, "确认收货失败，请稍后重试", null);
        int i = orderInfoMapper.updateStartsByOrderidToThree(orderId);
        if (i>0){
            jsonBean = new JsonBean(0, "确认收货成功", null);
        }
        return jsonBean;
    }

    @Override
    public JsonBean selectAllOrderInfoStatusYesZeroByUserId(Integer userid) {
        return new JsonBean(0,"",orderInfoMapper.selectAllOrderInfoStatusYesZeroByUserId(userid));
    }

    @Override
    public JsonBean selectAllOrderInfoStatusYesOneByUserId(Integer userid) {
        return new JsonBean(0,"",orderInfoMapper.selectAllOrderInfoStatusYesOneByUserId(userid));
    }

    @Override
    public JsonBean selectAllOrderInfoStatusYesTwoByUserId(Integer userid) {
        return new JsonBean(0,"",orderInfoMapper.selectAllOrderInfoStatusYesTwoByUserId(userid));
    }

    @Override
    public JsonBean selectAllOrderInfoStatusYesThreeByUserId(Integer userid) {
        return new JsonBean(0,"",orderInfoMapper.selectAllOrderInfoStatusYesThreeByUserId(userid));
    }
}
